var code = "";
var is_teacher="1";

var CONFIG = {
    regex: /^\d{11}$/ //11位数字手机号码
}
// 随屏幕高度变化 调整高度
function findHeight(){
    var obj = document.documentElement.clientHeight;
    $('.section.img-wrapper').height(obj);
}

function getValidate(){
    $("#validate_code").empty();
    var validate_len = 4;
    var ValidateNum = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    var dom = "";
    code = "";
    for(var i = 0;i<validate_len;i++){
        code = code + ValidateNum[parseInt(Math.random()*(ValidateNum.length))];
    }
    for(var i = 0;i<validate_len;i++){
        dom += "<span style='color:"+ getRandomColor() +"'>"+code[i]+"</span>";
    }
    $(dom).appendTo($("#validate_code"));
    var divContent = $("#validate_code").html();
    var data = "data:image/svg+xml," +
            "<svg xmlns='http://www.w3.org/2000/svg' width='100' height='40'>" +
            "<foreignObject width='100%' height='100%'>" +
            "<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:16px;font-family:Helvetica'>" +
            divContent +"</div>" +"</foreignObject>" +"</svg>";
    $("#validate_img").attr('src',data);
}
function getRandomColor(){
    var color=new Array("0","3","6","9","C","F");
    var fColor = "";
    for(var i=0;i<color.length;i++){
        fColor += color[parseInt(Math.random()*6)];
    }
    return "#"+fColor;
}

// 事件绑定初始化
function initEvent() {

    window.onresize = findHeight;

    // 直接登录带上referrer
    $('.goto-login').click(function() {
        window.location.href = '/Home/Index/login?redirect=' + document.referrer;
    });

    // 教员 家长 身份切换; 改样式
    $('.certify-option').click(function() {
        $('.certify-option').removeClass('active');
        $(this).addClass('active');
    });

    // 点击我是教员 我是家长；button文字 换
    $('.certify-tutor').click(function(event) {
        $('#sign_btn').text('教员注册');
    });
    $('.certify-parent').click(function(event) {
        $('#sign_btn').text('家长注册');
    });

    // getValidate();
    $("#validate_img").click(function(){
        getValidate();
    });

    if (is_teacher == "1") {
        $('.certify-tutor').click();
    };

    $('#getCode').btnTime({
        time: 60, //倒计时时间长度
        text: '重发', //按钮替换文字 重发(60秒)
        callback: function(e){
            //这里是你的业务逻辑代码
            $.ajax({
                type: "POST",
                url: "./phoneSalt",
                data: {
                    phone: $("#register_phone").val(),
                    type: 1
                },
                dataType: "json",
                success: function(data){
                    if(data.flag==1){
                        e();
                    }else{
                        alert(data.content);
                    }
                }
            });
        }
    });

    $('#sign_btn').click(function() {
        if (!validateForm()) return;
        var type = parseInt($('.certify-option.active').attr('type'));
        $.ajax({
            url: './registerjs',
            dataType: 'json',
            type: 'POST',
            data: {
                type: type,
                sms_salt: $('#register_code').val(),
                phone: $('#register_phone').val(),
                password: $('#register_pass').val(),
                code: $('#validate_input').val()
            }
        })
        .done(function(res) {
            if (res.error) {
                alert(res.msg)
                return;
            }
            if (type === 1) {
                window.location.href = '/Home/Teacher/center';
            }
            else {
                window.location.href = '/';
            }
        })
        ;
    });
}


function validateForm() {
    var register_phone = $('#register_phone').val();
    var register_pass = $('#register_pass').val()
    if (!CONFIG.regex.test(register_phone)) {
        alert('手机号码需11位小数');
        return false;
    }
    if (register_pass.length < 6) {
        alert('密码不得小于6位数');
        return false;
    }
    if (!$('#validate_input').val().length) {
        alert('图片验证码不得为空');
        return false;
    }
    if (!/^\d{6}$/.test($('#register_code').val())) {
        alert('手机验证码不能为空');
        return false;
    }
    return true;
}
$(function() {
    findHeight();

    initEvent();
})
